#include <clusnet/base/DynamicBufferLow.h>
#include <gtest/gtest.h>

#include <string>

using namespace clusnet::base;

TEST(DynamicBuffer, Test_append)
{
    DynamicBufferLow buffer(16);
    buffer.append("hello", 5);
    ASSERT_EQ(buffer.toString(), "hello");
    ASSERT_EQ(buffer.length(), 5);
    buffer.append("hello", 4);
    ASSERT_EQ(buffer.toString(), "hellohell");
    buffer.append("hello", 0);
    ASSERT_EQ(buffer.toString(), "hellohell");
    ASSERT_EQ(buffer.length(), 9);
}

class DynamicBufferLowTest : public testing::Test
{
    public:
        DynamicBufferLowTest() = default;
        virtual ~DynamicBufferLowTest() = default;
        DynamicBufferLowTest(const DynamicBufferLowTest&) = delete;
        DynamicBufferLowTest& operator= (const DynamicBufferLowTest&) = delete;

        virtual void SetUp() override
        {
            buffer.append("hello", 5);
        }

    protected:
        DynamicBufferLow   buffer;
};

TEST_F(DynamicBufferLowTest, Test_bzero)
{
    buffer.bzero();
    std::string zero(5, '\0');
    ASSERT_EQ(buffer.length(), 5);
    ASSERT_EQ(buffer.toString(), zero);
}

TEST_F(DynamicBufferLowTest, Test_reset)
{
    buffer.reset();
    ASSERT_EQ(buffer.length(), 0);
    ASSERT_TRUE(buffer.toString().empty());
}

TEST_F(DynamicBufferLowTest, Test_copyConstruct)
{
    DynamicBufferLow leftCopyBuffer(buffer);
    ASSERT_EQ(buffer.length(), leftCopyBuffer.length());
    ASSERT_EQ(buffer.toString(), leftCopyBuffer.toString());

    DynamicBufferLow rightCopyBuffer(std::move(leftCopyBuffer));
    ASSERT_EQ(buffer.length(), rightCopyBuffer.length());
    ASSERT_EQ(buffer.toString(), rightCopyBuffer.toString());
}

TEST_F(DynamicBufferLowTest, Test_assign)
{
    DynamicBufferLow leftAssignBuffer;
    leftAssignBuffer = buffer;
    ASSERT_EQ(buffer.length(), leftAssignBuffer.length());
    ASSERT_EQ(buffer.toString(), leftAssignBuffer.toString());

    DynamicBufferLow rightAssignBuffer;
    rightAssignBuffer = std::move(leftAssignBuffer);
    ASSERT_EQ(buffer.length(), rightAssignBuffer.length());
    ASSERT_EQ(buffer.toString(), rightAssignBuffer.toString());
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
